<template>
  <div>
    <!-- 搜索 -->
    <!-- 质量报告查询 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="100" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable ref="table" :columns="columns" :params="fetchParams" :dataSource="dataList" :datakey="'records'" :fetchapi="fetchApi" :onColumnsChange="columns => (this.columns = columns)" :onRowSelectChange="selectChange" :isSelectColumn="false">
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="visibleDetail" :title="'质量报告查询明细'" destroyOnClose width="70%" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <DetailForm @close="closeDetail" @save="saveDetail"  :rowData="rowData"></DetailForm>
    </Drawer>
    <BaseDialog :visible.sync="visible" :title="'车型选择'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <vehicle @close="close" @save="save"></vehicle>
    </BaseDialog>
    <BaseDialog :visible.sync="visibleAdd" :title="'质量报告反馈新增'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <addForm @close="closeAdd" @save="saveAdd"></addForm>
    </BaseDialog>
  </div>
</template>
<script>
import moment from "moment";
import {getListByPage,getListForExport,exporturl} from '@/api/claim/dealer/QualityInformationManagement_DLR/QualityInformationFeedback/index';
import DetailForm from './DetailForm.vue';
import vehicle from '../../BasicData/ClaimApplication/vehicle';
import { getPartType } from '@/api/dictionary/index';
import { exportExcelFile } from '@/utils/index';
import { dictionary } from '@/utils/dictMixin';
import { getAppId } from '@/assets/js/auth';

export default {
  mixins: [dictionary],
  components: {
    DetailForm,
    vehicle,
  },
  data() {
    return {
      dataList: [],
      visible: false,
      collapse: true,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchApi: getListByPage,
      fetchParams: {
        status:2,
        xhrAbort: true
      }, //ipa 接口
      selectedRows: [], //选中经销商
      visibleDetail: false,
      visibleAdd: false,
      rowData:{}
    };
  },
  created() {
    this.chuqi();
    this.fetchParams.applyBeginDate = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.applyEndDate = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
  },
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '质量报告单号',
          fieldName: 'reportNo',
          placeholder: '请输入',
          initialValue: ''
        }, {
          type: 'INPUT',
          label: '索赔单号',
          fieldName: 'modelCode2',
          placeholder: '请输入',
          initialValue: ''
        },

        {
          type: 'INPUT',
          label: '维修工单号',
          fieldName: 'cmsClaimNo',
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '主损件名称',
          fieldName: 'partName',
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '主损件代码',
          fieldName: 'partCode',
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelCode',
          readonly: true, // 只读
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.modelTurnOn()}/>
              </div>
            );
          }
        },
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'applyBeginDate|applyEndDate',
          placeholder: '请输入',
          initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],
          rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        {
          type: 'SELECT',
            label: '质量报告状态',
          fieldName: 'reportStatus',
          itemList: this.createDictList('5561',[55611004,55611007,55611008]),
          readonly: true // 只读
        },
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      if(!val.applyBeginDate) {
        this.$notify.warning({ title: '提示信息', message: '创建开始日期不可为空！' });
        return
      }
      if(!val.applyEndDate) {
        this.$notify.warning({ title: '提示信息', message: '创建结束日期不可为空！' });
        return
      }
      if(getAppId() == 'arcfox') {
        if (moment(moment(val.applyEndDate).format('YYYY-MM-DD')).diff(moment(val.applyBeginDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return
        }
      } else {
        if (moment(moment(val.applyEndDate).format('YYYY-MM-DD')).diff(moment(val.applyBeginDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return
        }
      }
      this.fetchParams = Object.assign({},val,{
        status:2,
        xhrAbort: false
      });
      console.log(val);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: props => {
            return (
              <div style="text-align:center">
            <el-button size="mini" type="text" onClick={() => this.detail(props.row)}>
            查看
            </el-button>

              </div>
            );
          }
        },
        { title: '质量报告状态', dataIndex: 'statusStr', sorter: true },
        { title: '质量报告单号', dataIndex: 'reportNo', sorter: true },
        { title: '索赔单号', dataIndex: 'cmsClaimNo', sorter: true },
        { title: '维修工单号', dataIndex: 'roNo', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '生产日期', dataIndex: 'prodDate', sorter: true,dateFormat: 'yyyy-MM-dd' },
        { title: '销售日期', dataIndex: 'saleDate', sorter: true ,dateFormat: 'yyyy-MM-dd'},
        { title: '车型名称', dataIndex: 'modelName', sorter: true },
        { title: '主损件代码', dataIndex: 'partCode', sorter: true },
        { title: '主损件名称', dataIndex: 'partName', sorter: true },
        { title: '行驶里程', dataIndex: 'driveMile', sorter: true },
        { title: '故障现象代码', dataIndex: 'faultCode', sorter: true },
        { title: '故障现象名称', dataIndex: 'faultName', sorter: true },
        // { title: '故障原因代码', dataIndex: 'sysCode4', sorter: true },
        // { title: '故障原因名称', dataIndex: 'sysName4', sorter: true },
        { title: '维修开始时间', dataIndex: 'serviceBeginDate', sorter: true },
        { title: '维修结束时间', dataIndex: 'serviceEndDate', sorter: true },
        { title: '申请时间', dataIndex: 'applyAt', sorter: true },
        { title: '申请人', dataIndex: 'applyBy', sorter: true },
        { title: '审核时间', dataIndex: 'checkAt', sorter: true },
        { title: '审核人', dataIndex: 'checkBy', sorter: true }
      ];
    },
    async chuqi(){
      // console.log("进来了")
      // const res1 = await getPartType( {type:5561});
      // if (res1.resultCode == 200) {
      //   delete res1.data[3];
      //   delete res1.data[5];
      //   delete res1.data[6];
      //   delete res1.data[7];
      //   this.topFilterInit.find(x => x.fieldName == 'reportStatus').initialValue = '';
      //   this.topFilterInit.find(x => x.fieldName == 'reportStatus').itemList = res1.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      // }
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    modelTurnOn() {
      this.visible = true;
    },
   async exportFileBtn() {
           let json = Object.assign({}, { CODE: 'CODE_00000211' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "applyBeginDate|applyEndDate").initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == "applyBeginDate|applyEndDate").initialValue[1];
      // if(!beginSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '创建开始日期不可为空！' });
      //   return
      // }
      // if(!endSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '创建结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, { status:2,xhrAbort: false });
      // obj.applyBeginDate = this.topFilterInit.find(x => x.fieldName == "applyBeginDate|applyEndDate").initialValue[0];
      // obj.applyEndDate = this.topFilterInit.find(x => x.fieldName == "applyBeginDate|applyEndDate").initialValue[1];
      // getListForExport(obj).then(res => {
      //   exportExcelFile(res.data, '质量报告信息');
      // });
    },
    detail(val) {
      this.rowData = val;
      this.visibleDetail = true;
    },
    closeDetail() {
      this.visibleDetail = false;
    },
    saveDetail() {
      this.visibleDetail = false;
      this.fetchParams = { ...this.fetchParams };
    },
    close() {
      this.visible = false;
    },
    save(obj) {
      this.visible = false;
      this.topFilterInit.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
    },
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd(obj) {
      this.visibleAdd = false;
    },
  }
  // mounted() {
  //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
  // }
};
</script>

<style lang="less" scoped></style>
